package edu.tjufe.llp.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.tjufe.llp.common.Result;
import edu.tjufe.llp.entity.ChargeArea;
import edu.tjufe.llp.service.IChargeAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 充电区域Controller
 *
 * @author leiliping
 * @date 2022-12-24
 */
@RestController
@RequestMapping("/area")
public class ChargeAreaController
{
    @Autowired
    private IChargeAreaService chargeAreaService;

    /**
     * mp分页222
     */
    @GetMapping("/list")
    public Result getAreaList(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam(defaultValue = "") String areaName){
        IPage<ChargeArea> page = new Page<>(pageNum,pageSize);
        QueryWrapper<ChargeArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("area_name",areaName);
        return Result.success(chargeAreaService.page(page,queryWrapper));
    }
//    @GetMapping("/areaList")
//    public Result getAreaList(@RequestParam Integer pageNum,
//                              @RequestParam Integer pageSize){
//        IPage<ChargeArea> page = new Page<>(pageNum,pageSize);
////        QueryWrapper<ChargeArea> queryWrapper = new QueryWrapper<>();
////        queryWrapper.like("areaName",areaName);
////        queryWrapper.like("freeTime",freeTime);
//        return Result.success(chargeAreaService.page(page,null));
//    }

    /**
     * 分页查询mp
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String areaName){
        return Result.success(chargeAreaService.findPage(new Page<>(pageNum, pageSize), areaName));
    }
    /**
     * 查询充电区域列表
     */
    @GetMapping("/list2")
    public Result findAll(ChargeArea chargeArea) {
        List<ChargeArea> list = chargeAreaService.selectChargeAreaList(chargeArea);
        Integer total = chargeAreaService.selectTotal();
        Map<String,Object> res = new HashMap<>();
        res.put("data",list);
        res.put("total",total);
        return Result.success(res);
    }

    /**
     * 获取充电场所详细信息
     */
    @GetMapping(value = "/{areaId}")
    public Result getInfo(@PathVariable("areaId") Integer areaId)
    {
        return Result.success(chargeAreaService.selectChargeAreaByAreaId(areaId));
    }

    /**
     * 新增充电区域
     */
    @PostMapping
    public Result add(@RequestBody ChargeArea chargeArea)
    {
        return Result.toJudge(chargeAreaService.insertChargeArea(chargeArea));
    }

    /**
     * 修改充电区域
     */
    @PutMapping
    public Result edit(@RequestBody ChargeArea chargeArea)
    {
        return Result.toJudge(chargeAreaService.updateChargeArea(chargeArea));
    }

    /**
     * 删除充电区域
     */
    @DeleteMapping("/{areaIds}")
    public Result remove(@PathVariable Integer[] areaIds)
    {
        return Result.toJudge(chargeAreaService.deleteChargeAreaByAreaIds(areaIds));
    }

    /**
     * 导出充电桩
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        // 从数据库查询出所有的数据
        List<ChargeArea> list = chargeAreaService.list();
        // 通过工具类创建writer 写出到磁盘路径
//        ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用户信息.xlsx");
        // 在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //自定义标题别名
        writer.addHeaderAlias("areaId", "区域id");
        writer.addHeaderAlias("areaName", "区域名称");
        writer.addHeaderAlias("freeTime", "免费时长");
        writer.addHeaderAlias("timeUnit", "计费单元");
        writer.addHeaderAlias("unitCost", "单元费用");
        writer.addHeaderAlias("maxCost", "最大收费");
        writer.addHeaderAlias("remark", "备注");

        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(list, true);

        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("充电区域信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();

    }

}
